import { defineStore } from 'pinia'
import {
    getPatternList,
    addPattern,
    updatePattern,
    deletePattern,
    refreshCache,
    updatePatternStatus,
    deletePatternBatch

} from '@/api/pattern'

export const usePatternStore = defineStore('pattern', {
    state: () => ({
        patterns: [],
        loading: false,
        message: '',
        currentPattern: null,
        pagination: {
            current: 1,
            size: 10,
            total: 0
        },
        searchFieldType: '',
        selectedPatterns: [],
    }),

    actions: {
        // 设置选中的规则
        setSelectedPatterns(patterns) {
            this.selectedPatterns = patterns
        },

        // 批量删除
        async batchDeletePatterns(ids) {
            try {
                await deletePatternBatch(ids)
                // 刷新列表
                await this.fetchPatterns({
                    current: this.pagination.current,
                    size: this.pagination.size,
                    fieldType: this.searchFieldType
                })
                this.selectedPatterns = []
                return true
            } catch (error) {
                this.message = '批量删除失败: ' + error.message
                return false
            }
        },

        // 批量更新状态
        async batchUpdateStatus(ids, enabled) {
            try {
                await updatePatternStatus(ids, enabled);
                // 刷新列表
                await this.fetchPatterns({
                    current: this.pagination.current,
                    size: this.pagination.size,
                    fieldType: this.searchFieldType
                })
                this.selectedPatterns = []
                return true
            } catch (error) {
                this.message = '批量更新状态失败: ' + error.message
                return false
            }
        },

        // 设置筛选条件并刷新数据
        async setFilter(fieldType) {
            this.searchFieldType = fieldType
            this.pagination.current = 1  // 重置到第一页
            await this.fetchPatterns({
                current: 1,
                size: this.pagination.size,
                fieldType: fieldType
            })
        },

        async fetchPatterns(params = {}) {
            this.loading = true
            try {
                const { current = this.pagination.current,
                    size = this.pagination.size,
                    fieldType= params.fieldType ?? this.searchFieldType } = params

                const response = await getPatternList({ current, size, fieldType })
                this.patterns = response.data.data.records
                this.pagination = {
                    current: response.data.data.current,
                    size: response.data.data.size,
                    total: response.data.data.total
                }
            } catch (error) {
                this.message = '获取规则列表失败: ' + error.message
            } finally {
                this.loading = false
            }
        },

        async addPattern(pattern) {
            try {
                await addPattern(pattern)
                // 刷新列表
                await this.fetchPatterns({
                    current: this.pagination.current,
                    size: this.pagination.size,
                    fieldType: this.searchFieldType
                })
                return true
            } catch (error) {
                this.message = '添加规则失败: ' + error.message
                return false
            }
        },

        async updatePattern(pattern) {
            try {
                await updatePattern(pattern.id, pattern)
                // 刷新列表
                await this.fetchPatterns({
                    current: this.pagination.current,
                    size: this.pagination.size,
                    fieldType: this.searchFieldType
                })
                return true
            } catch (error) {
                this.message = '更新规则失败: ' + error.message
                return false
            }
        },

        async deletePattern(id) {
            try {
                await deletePattern(id)
                // 刷新列表
                await this.fetchPatterns({
                    current: this.pagination.current,
                    size: this.pagination.size,
                    fieldType: this.searchFieldType
                })
                return true
            } catch (error) {
                this.message = '删除规则失败: ' + error.message
                return false
            }
        },

        async refreshPatternCache() {
            try {
                await refreshCache()
                this.message = '刷新缓存成功'
                return true
            } catch (error) {
                this.message = '刷新缓存失败: ' + error.message
                return false
            }
        },

        setSearchFieldType(fieldType) {
            this.searchFieldType = fieldType
            this.pagination.current = 1 // 重置到第一页
            this.fetchPatterns({ current: 1, fieldType })
        },

        setPage(current) {
            this.pagination.current = current
            this.fetchPatterns({ current })
        },

        setPageSize(size) {
            this.pagination.size = size
            this.pagination.current = 1
            this.fetchPatterns({ current: 1, size })
        }
    }
})